package com.udaos.modules.sys.controllers;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.udaos.common.base.Globals;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.shiro.view.Permission;
import com.udaos.models.sys.Sys_menu;
import com.udaos.modules.sys.services.SysMenuService;

/**
 * 首页
 * 
 * @author mutaotao
 *
 */
@IocBean
@At("/platform/home")
@Filters({ @By(type = PrivateFilter.class) })
public class SysHomeController {
	private static final Logger logger = LoggerFactory.getLogger(SysHomeController.class);
	@Inject
	SysMenuService menuService;

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/desktop/desktop.html")
	@RequiresAuthentication
	public void home() {

	}

	/**
	 * 初始化菜单数据
	 * 
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object getMenuData(String property) {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			Object principal = subject.getPrincipal();
			try {
				BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
				for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
					if (pd.getName().equals(property) == true) {
						return pd.getReadMethod().invoke(principal, (Object[]) null);
					}
				}
				logger.trace("Property [{}] not found in principal of type [{}]", property,principal.getClass().getName());
			} catch (Exception e) {
				logger.trace("Error reading property [{}] from principal of type [{}]", property,principal.getClass().getName());
			}
		}
		return null;
	}

	@At
	@Ok("beetl:/platform/sys/left.html")
	@RequiresAuthentication
	public void left(@Param("url") String url, HttpServletRequest req) {
		String path = "";
		String perpath = "";
		if (!Strings.isBlank(Globals.AppBase)) {
			url = Strings.sBlank(url).substring(Globals.AppBase.length());
		}
		if (Strings.sBlank(url).indexOf("?") > 0)
			url = url.substring(0, url.indexOf("?"));
		Sys_menu menu = menuService.fetch(Cnd.where("href", "=", url));
		if (menu != null) {
			if (menu.getPath().length() > 8) {
				path = menu.getPath().substring(0, 8);
				perpath = menu.getPath().substring(0, 4);
			} else if (menu.getPath().length() == 8) {
				perpath = menu.getPath().substring(0, 4);
			}
			req.setAttribute("mpath", menu.getPath());
		}
		req.setAttribute("path", path);
		req.setAttribute("perpath", perpath);
	}

	@At
	@Ok("beetl:/platform/sys/left.html")
	@RequiresAuthentication
	public void path(@Param("url") String url, HttpServletRequest req) {
		if (Strings.sBlank(url).indexOf("//") > 0) {
			String[] u = url.split("//");
			String s = u[1].substring(u[1].indexOf("/"));
			if (Strings.sBlank(s).indexOf("?") > 0)
				s = s.substring(0, s.indexOf("?"));
			if (!Strings.isBlank(Globals.AppBase)) {
				s = s.substring(Globals.AppBase.length());
			}
			String[] urls = s.split("/");
			List<String> list = new ArrayList<>();
			if (urls.length > 5) {
				list.add("/" + urls[1] + "/" + urls[2] + "/" + urls[3] + "/" + urls[4] + "/" + urls[5]);
				list.add("/" + urls[1] + "/" + urls[2] + "/" + urls[3] + "/" + urls[4]);
				list.add("/" + urls[1] + "/" + urls[2] + "/" + urls[3]);
				list.add("/" + urls[1] + "/" + urls[2]);
				list.add("/" + urls[1]);
			} else if (urls.length == 5) {
				list.add("/" + urls[1] + "/" + urls[2] + "/" + urls[3] + "/" + urls[4]);
				list.add("/" + urls[1] + "/" + urls[2] + "/" + urls[3]);
				list.add("/" + urls[1] + "/" + urls[2]);
				list.add("/" + urls[1]);
			} else if (urls.length == 4) {
				list.add("/" + urls[1] + "/" + urls[2] + "/" + urls[3]);
				list.add("/" + urls[1] + "/" + urls[2]);
				list.add("/" + urls[1]);
			} else if (urls.length == 3) {
				list.add("/" + urls[1] + "/" + urls[2]);
				list.add("/" + urls[1]);
			} else if (urls.length == 2) {
				list.add("/" + urls[1]);
			} else
				list.add(url);
			String path = "";
			String perpath = "";
			Sys_menu menu = menuService.fetch(Cnd.where("href", "in", list).desc("href").desc("path"));
			if (menu != null) {
				if (menu.getPath().length() > 8) {
					path = menu.getPath().substring(0, 8);
					perpath = menu.getPath().substring(0, 4);
				} else if (menu.getPath().length() == 8) {
					perpath = menu.getPath().substring(0, 4);
				}
				req.setAttribute("mpath", menu.getPath());
			}
			req.setAttribute("path", path);
			req.setAttribute("perpath", perpath);
		}
	}

	@At(value = { "/", "/index" }, top = true)
	@Ok(">>:/sysadmin")
	public void index() {
	}
}
